<?php
/**

 */
class Admin_ClientController extends App_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
		//$form = new Form_ClientLogin();
		$form = new Form_ManagerForm();
		$this->view->form = $form;

		$form2=new Form_SearchAccountForm();
		$this->view->form2=$form2;
		
		$user_id = $this->getUserId();
		$user_role = 'admin';
		
    	$request=$this->getRequest();
		$data = $request->getParams();
				
		$model_client = new Model_UserClient();
		
		$act = $request->getParam('act');
		if (isset($act)and($act=='disable')){
			$id = $request->getParam('id');		
			$result = $model_client->disableClient($id);	
			if ($result)
			{
			   $this->view->success_msg="Client has been disabled!";
			}else{
			   $this->view->error_msg="Sorry, disable client failed.";					
			}		
		}
		if (isset($act)and($act=='suspend')){
			$id = $request->getParam('id');		
			$result = $model_client->suspendClient($id);	
			if ($result)
			{
			   $this->view->success_msg="Client has been suspended!";
			}else{
			   $this->view->error_msg="Sorry, suspend client failed.";					
			}		
		}
		if (isset($act)and($act=='activate')){
			$id = $request->getParam('id');		
			$result = $model_client->activateClient($id);	
			if ($result)
			{
			   $this->view->success_msg="Client has been activated!";
			}else{
			   $this->view->error_msg="Sorry, activate client failed.";					
			}		
		}
		
		$start = $request->getParam('page',1);
		$email = $request->getParam('email');
		$dpt_id = $request->getParam('dpt_id');
		$dpt_account_login = $request->getParam('dpt_account_login');
		$login_area = $request->getParam('login_area');
		$role = $request->getParam('role');
		
		$item_per_page = 20;		
		$data['start'] = ($start-1)*$item_per_page;
		$data['limit'] = $item_per_page;
		$this->view->start=$data['start'];
		//if($request->isPost())
		{
/*			
			if (isset($data['submit']) and ($data['submit']=='Add Client'))
			{
				$dpt_id=$data['dpt_id'];
				
				$dpt_account_login = $data['username'];
			
				$dpt_password=$data['password'];
				
				if ($dpt_id==0) // create central account
				{
					$client_id = $model_client->signupClient($dpt_account_login, $dpt_password);
					if ($client_id)
					{
					   $this->view->success_msg="New client has been added Successfully!";
					}else{
					   $this->view->error_msg="Sorry, add new client failed.";					
					}		
					
				}else{
					$dpt_login = $model_client->dptLoginCheck($dpt_id, $dpt_account_login, $dpt_password);
					if ($dpt_login['status'] === true)
					{
						$client_id = $model_client->getClientIDByService($dpt_id, $dpt_account_login);						

						if ($client_id){
						   $this->view->error_msg="Sorry, The client associated with this account already exist, client id: <b>$client_id</b>";											
						}else{		
							$dpt_login_arr=array();
							$dpt_login_arr['dpt_account_login'] = $dpt_account_login;
							$dpt_login_arr['login_area'] = $dpt_login['login_area'];
							$dpt_login_arr['email'] = $dpt_login['email'];
							$dpt_account_id = $dpt_login['dpt_account_id'];
							$client_id=false;			
							$client_id = $model_client->linkClientService($client_id, $dpt_id, $dpt_account_id, $dpt_login_arr);							
							if ($client_id)
							{
							   $this->view->success_msg="New client has been added Successfully!";
							}else{
							   $this->view->error_msg="Sorry, add new client failed.";					
							}		
						}											
					}else{
					   $this->view->error_msg="Sorry, Invalid username and/or password.";										
					}
				}
*/
			if (isset($data['submit']) and ($data['submit']=='Set'))
			{
				$model_service = new Model_Service();
		    	$all_services = $model_service->getAllService();
		    	$dpt_ids_str = '';
		        foreach($all_services as $key =>$item){
		        	if (isset($data['dpt_id_'.$item['dpt_id']]) and ($data['dpt_id_'.$item['dpt_id']]==1)){
		        		$dpt_ids_str .= ','.$item['dpt_id'];
		        	}
		        }
		        
				$client_id = $data['manager_client_id'];
			
				if (strlen($dpt_ids_str)<=0)
				{
					$result = $model_client->unsetManager($client_id);
					if ($result)
					{
						$this->view->success_msg="Unset Manager Successfully!";
					}else{
					   $this->view->error_msg="Sorry, unset failed.";										
					}
				}else{
			        if (strlen($dpt_ids_str)>0)
						$dpt_ids_str = substr($dpt_ids_str,1,strlen($dpt_ids_str)-1);
					
					$result = $model_client->setManager($client_id, $dpt_ids_str);
					if ($result)
					{
						$this->view->success_msg="Manager has been set Successfully!";
					}else{
					   $this->view->error_msg="Sorry, set manager failed.";										
					}
				}
			
			
			}else{
				if ($email)
				{
					$data['filters'][0]['field'] = 'email';
					$data['filters'][0]['data']['type'] = 'string';  //string, list, boolean, numeric, date
					$data['filters'][0]['data']['value'] = $email;
				}

				if (($dpt_id)and($dpt_id!=-1))
				{
					$data['filters'][1]['field'] = 'dpt_id';
					$data['filters'][1]['data']['type'] = 'string';  //string, list, boolean, numeric, date
					$data['filters'][1]['data']['value'] = $dpt_id;
				}
				
				if ($dpt_account_login)
				{
					$data['filters'][2]['field'] = 'dpt_account_login';
					$data['filters'][2]['data']['type'] = 'string';  //string, list, boolean, numeric, date
					$data['filters'][2]['data']['value'] = $dpt_account_login;
				}
				
				if (($login_area)and($login_area!='any'))
				{
					$data['filters'][3]['field'] = 'login_area';
					$data['filters'][3]['data']['type'] = 'string';  //string, list, boolean, numeric, date
					$data['filters'][3]['data']['value'] = $login_area;
				}
				
				if (($role)and($role!='any'))
				{
					$data['filters'][4]['field'] = 'role';
					$data['filters'][4]['data']['type'] = 'string';  //string, list, boolean, numeric, date
					$data['filters'][4]['data']['value'] = $role;
				}
				
				$form2->populate($data);
				
				
			}
			
		}
				
		$all_clients = $model_client->getAllClient($data);   
		$this->view->all_clients=$all_clients['data'];
		//Zend_Debug::dump($this->view->all_clients);exit;
		
        $model_service = new Model_Service();
    	$all_services = $model_service->getAllService();                
		$this->view->all_services=$all_services;        
				
		
		$_GET['page']=$start;
		$_GET['email']=$email;
		$_GET['dpt_id']=$dpt_id;
		$_GET['dpt_account_login']=$dpt_account_login;
		$_GET['login_area']=$login_area;
		$_GET['role']=$role;
		
		Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
    	
    	$paginator = Zend_Paginator::factory($all_clients['total']);
    	$paginator->setItemCountPerPage($item_per_page); //num per page
    	$paginator->setCurrentPageNumber($start);
    	$this->view->paginator = $paginator;		
		    	
    }


    public function editAction()
    {
    	$request=$this->getRequest();  
		$user_id = $this->getUserId();
    	
		$success_msg = $request->getParam('success_msg');
		if (isset($success_msg)and(!empty($success_msg)))
			$this->view->success_msg=$success_msg;
			
		$error_msg = $request->getParam('error_msg');
		if (isset($error_msg)and(!empty($error_msg)))
			$this->view->error_msg=$error_msg;
			
    	$act = $request->getParam('act');   //get action 	
    	
      	$client_id = $request->getParam('id');    	
		$user_role = 'client';

		$model_client = Model_UserFactory::create($user_role);

		if (isset($act)and($act=='remove')){
    		$dpt_id = $request->getParam('dpt_id');  	
    		$dpt_account_id = $request->getParam('dpt_account_id');  	
    		$client_id = $request->getParam('client_id');

			$dpt_login_arr=array();
			$dpt_login_arr['admin_user_id'] = $user_id;
			$dpt_login_arr['url'] = $_SERVER['REQUEST_URI'];
    		
    		$result=$model_client->removeClientService($client_id, $dpt_id, $dpt_account_id, $client_id, $dpt_login_arr);
			if ($result)
			{
				$this->view->success_msg="Service has been removed Successfully!";
				$this->_redirect("/admin/client/edit/id/$client_id/success_msg/Service has been removed Successfully!");
			}else{
				$this->view->error_msg="Sorry, service removed failed.";
				$this->_redirect("/admin/client/edit/id/$client_id/error_msg/Sorry, service removed failed.");
			}    		
		}else{
		
			if($request->isPost()){
				$data = $request->getParams();
				
				$login_account = $data['account_id']; //spliting processed in login check
			
				$password=$data['password'];
				$dpt_id=$data['dpt_id'];
				
				$dpt_check = $model_client->dptLoginCheck($dpt_id, $login_account, $password);
				if ($dpt_check['status']===true){
					$dpt_login_arr=array();
					$dpt_login_arr['dpt_account_login'] = $login_account;
					$dpt_login_arr['login_area'] = $dpt_check['login_area'];
					$dpt_login_arr['email'] = $dpt_check['email'];
					$dpt_login_arr['password'] = $password;
					$dpt_login_arr['admin_user_id'] = $user_id;
					$dpt_login_arr['url'] = $_SERVER['REQUEST_URI'];
					$dpt_account_id = $dpt_check['dpt_account_id'];
				
					$result = $model_client->linkClientService($client_id, $dpt_id, $dpt_account_id, $dpt_login_arr);
					if ($result)
					{
						$this->view->success_msg="Service has been subscribed Successfully!";
					}else{
						$this->view->error_msg="Sorry, service subscribe failed.";
					}
				}else{
					$this->view->error_msg="Sorry, account validation failed: ".$dpt_check['message'];
				}						
			}
		}
		
		$client_info = $model_client->getClient($client_id);
		//Zend_Debug::dump($client_info);exit;		
		$this->view->client_info=$client_info;
		
		$model_service = new Model_Service();
    	$all_services = $model_service->getAllService();   					
		$client_services = $model_client->getClientService($client_id);
	
		$final_client_services = array();
		$pre_dpt_id=0;
		foreach ($client_services as $key => $item ){
			$dpt_id=$item['dpt_id'];
			if ($dpt_id==$pre_dpt_id){
				$final_client_services[ $pre_dpt_id ][]=$item;
			}else{
				$final_client_services[ $dpt_id ][]=$item;				
			}
			$pre_dpt_id = $dpt_id;
		}
		
		
		$this->view->client_id=$client_id;
		$this->view->all_services=$all_services;
		$this->view->client_services=$final_client_services;
    }
    
    
	public function logoutAction()
	{
		$session = $this->getSession();
		$session->unsetAll();

		$this->_redirect('/');
	}

    
}